package com.coship.vasms.adstorm.common;

import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class CommonUtils {
	private static String config_path;

	private static String config_dir = "config";

	private final static String enc = System.getProperty("file.encoding");

	private final static String FS = System.getProperty("file.separator");

	private final static String lSeparator = System
			.getProperty("line.separator");

	private final static boolean isGBK = enc.indexOf("GB") >= 0;

	private static String hostName = null;

	private static String hostIP = null;
	// tvweb 发布web包的绝对目录,系统启动时初始化该变量
	private static String webPackageDir;

	public static String getServerEncode() {
		return enc;
	}

	/**
	 * 以GBK为标准，返回服务器系统的编码变量的boolean型
	 * 
	 * @return boolean true ---是GBK编码 false---非GBK编码
	 */
	public static boolean isGBKsystem() {
		return isGBK;
	}

	public static String getUserDir() {

		return System.getProperty("user.dir");
	}

	/**
	 * 获取服务器的文件分割符
	 * 
	 * @return String
	 */
	public static String getSysFileSeparator() {
		return FS;
	}

	/**
	 * 获取服务器文件换行符
	 * 
	 * @return String
	 */
	public static String getLineSeparator() {
		return lSeparator;
	}

	/**
	 * 获取服务器所在机器IP地址
	 * 
	 * @throws UnknownHostException
	 * @return String IP地址
	 */
	public static String getLocalIP() throws UnknownHostException {
		if (hostIP == null) {
			hostIP = java.net.InetAddress.getLocalHost().getHostAddress();
		}
		return hostIP;
	}

	/**
	 * 获取服务器所在主机的主机名称
	 * 
	 * @throws UnknownHostException
	 *             作为common下的util,不作任何异常处理，直接抛出
	 * @return String
	 */
	public static String getHostName() throws UnknownHostException {
		if (hostName == null) {
			hostName = java.net.InetAddress.getLocalHost().getHostName();
		}
		return hostName;
	}

	/**
	 * 获取配置文件路径的方法
	 * 
	 * @return String
	 */
	public static String getConfigPath() {
		if (StringUtils.isEmpty(config_path)) {
			config_path = CommonUtils.getUserDir()
					+ CommonUtils.getSysFileSeparator() + config_dir;
			return config_path;
		}

		return config_path;
	}

	/**
	 * 获取配置文件路径的方法
	 * 
	 * @return String
	 */
	public static void setConfigPath(String configPath) {
		config_path = configPath;
	}

	public static void setConfigDir(String configDir) {
		config_dir = configDir;
	}

	/**
	 * 
	 * Description : 获取当前时间字段串,精确到毫秒,格式yyyyMMddHHmmSSS
	 * 
	 * @return
	 * 
	 */
	public static String getCurrentTimeByMinosecd() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmSSS");
		return simpleDateFormat.format(new Date());
	}

	public static void main(String args[]) {
		System.out.println("config path=" + getConfigPath());
	}

	/**
	 * @return the webPackageDir
	 */
	public static String getWebPackageDir() {
		return webPackageDir;
	}

	/**
	 * @param webPackageDir
	 *            the webPackageDir to set
	 */
	public static void setWebPackageDir(String webPackageDir) {
		CommonUtils.webPackageDir = webPackageDir;
	}

	/**
	 * 半角转全角
	 * 
	 * @param input
	 *            String.
	 * @return 全角字符串.
	 */
	public static String toSBC(String input) {
		if (input == null) {
			return null;
		}
		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == ' ') {
				c[i] = '\u3000';
			} else if (c[i] < '\177') {
				c[i] = (char) (c[i] + 65248);

			}
		}
		return new String(c);
	}

	/**
	 * 全角转半角
	 * 
	 * @param input
	 *            String.
	 * @return 半角字符串
	 */
	public static String toDBC(String input) {
		if (input == null) {
			return null;
		}
		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == '\u3000') {
				c[i] = ' ';
			} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
				c[i] = (char) (c[i] - 65248);

			}
		}
		String returnString = new String(c);

		return returnString;
	}
}
